﻿namespace OboutInc.EasyMenu_Pro
{
    using System;
    using System.Web.UI;

    internal class ScriptManagerHelperAJAXPage
    {
        private object AJAXSM;
        private bool isAJAXPage;
        private Type smType;

        public ScriptManagerHelperAJAXPage(Control control)
        {
            if (control.Page != null)
            {
                this.smType = control.Page.GetType();
                while (this.smType.FullName != "System.Web.UI.Page")
                {
                    if (this.smType.FullName == "OboutInc.oboutAJAXPage")
                    {
                        this.isAJAXPage = true;
                        this.AJAXSM = control.Page;
                        this.smType = this.AJAXSM.GetType();
                        return;
                    }
                    this.smType = this.smType.BaseType;
                }
            }
        }

        public void RegisterScript(Control control, string ScriptKey, string Script)
        {
            if (this.IsAJAXPage)
            {
                this.smType.GetMethod("RegisterScript", new Type[] { typeof(string), typeof(string) }).Invoke(this.AJAXSM, new object[] { ScriptKey, Script });
            }
        }

        public void RegisterScriptFile(Control control, string ScriptFileKey, string ScriptFilePath)
        {
            if (this.IsAJAXPage)
            {
                this.smType.GetMethod("RegisterScriptFile", new Type[] { typeof(string), typeof(string) }).Invoke(this.AJAXSM, new object[] { ScriptFileKey, ScriptFilePath });
            }
        }

        public void RegisterStyleFile(Control control, string StyleFileKey, string StyleFilePath)
        {
            if (this.IsAJAXPage)
            {
                this.smType.GetMethod("RegisterStyleFile", new Type[] { typeof(string), typeof(string) }).Invoke(this.AJAXSM, new object[] { StyleFileKey, StyleFilePath });
            }
        }

        public bool IsAJAXPage
        {
            get
            {
                return this.isAJAXPage;
            }
        }

        public bool IsInAsyncCallback
        {
            get
            {
                return (this.IsAJAXPage && bool.Parse(this.smType.GetProperty("IsCallback").GetValue(this.AJAXSM, null).ToString()));
            }
        }
    }
}

